/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package progweb.project.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import progweb.project.Manager.DBManager;
import progweb.project.Manager.GUIManager;
import org.apache.log4j.*;

/**
 *
 * @author Admin
 */
public class RegisterServLet extends HttpServlet {
    private static org.apache.log4j.Logger log=Logger.getLogger(RegisterServLet.class);
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        GUIManager gui = new GUIManager(out);
        
        log.info("Recupero parametri dalla request");
        /*prende parametri fname, lname e pwd dal form di registrazione*/
        String fname = request.getParameter("fname");
        String lname = request.getParameter("lname");
        String pwd = request.getParameter("pwd");
        
        /*registrazione del paziente e stampa della pagina di avvenuta registrazione*/
        try {
            DBManager.updatePassword(fname, lname, pwd);
            log.info("Paziente registrato");
            String href = "index.html";
            String msg = "Benvenuto "+fname+" "+lname+", ti sei registrato con successo!<br/>"
                    + "Puoi effetuare il login al <a href=\"'"+href+"'>seguente indirizzo</a>";
            gui.printRegistrationPage(msg, href);
        } catch (Exception ex) {
            log.error("Errore durante la registrazione del paziente");
            request.setAttribute("error", "Riprovare: errore durante la registrazione.");
            request.setAttribute("page", "registrazione.html");
            RequestDispatcher rd=request.getRequestDispatcher("/ErrorServlet");
            rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
